#include "mainwindow.h"
#include <iostream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setupUi(this);
    this->setWindowTitle(QString::fromUtf8("课程表"));
    this->tableWidget->setRowCount(12);
    this->tableWidget->setColumnCount(7);
    loadClasses();

    connect(this->bt_setCalender,SIGNAL(clicked()),this,SLOT(showSetSchoolDialog()));
    connect(this->bt_addCourse,SIGNAL(clicked()),this,SLOT(showAddCourse()));
    connect(this->tableWidget,SIGNAL(itemClicked(QTableWidgetItem*)),this,SLOT(showUpdataCourse(QTableWidgetItem*)));
    connect(&addCourse,SIGNAL(addcoursefinish()),this,SLOT(loadClasses()));
    connect(&dialog,SIGNAL(schoolcalchange()),this,SLOT(loadClasses()));
    connect(&updataCourse,SIGNAL(deletefinish()),this,SLOT(loadClasses()));
    connect(&updataCourse,SIGNAL(updatafinish()),this,SLOT(loadClasses()));
}

void MainWindow::loadClasses()        
{
    //现获取开学时间计算当前是第几周
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");//数据库查询
    db.setHostName(HOSTNAME);
    db.setUserName(USERNAME);
    db.setPassword(PASSWORD);
    db.setDatabaseName(DATABASENAME);
    if(!db.open())
    {
        QMessageBox::critical(0,QObject::tr("数据库出错"),db.lastError().text());
        return ;
    }
    QSqlQuery query;
    query.exec("select thatdate from schoolcal");
    while(query.next())
    {
        date=query.value(0).toDate();
    }
    int days=date.daysTo(QDate::currentDate());
    week=1+days/7;
    db.close();
    this->lb_showCalendar->setText(QString::fromUtf8(QDate::currentDate().toString().toUtf8()+" 第 "+QString::number(week).toUtf8()+" 周"));
    getclasses();
}

void MainWindow::getclasses()
{
    //获取符合条件的课程
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");//数据库查询
    db.setHostName(HOSTNAME);
    db.setUserName(USERNAME);
    db.setPassword(PASSWORD);
    db.setDatabaseName(DATABASENAME);
    if(!db.open())
    {
        QMessageBox::critical(0,QObject::tr("数据库出错"),db.lastError().text());
        return ;
    }
    QSqlQuery query;
    query.exec("select classname,classroom,weekday,coursenumber,coursecount from classinfo where weekstart<="+QString::number(week)+" and weekend>="+QString::number(week));

    this->tableWidget->clear();
    while(query.next())
    {        
        QString message = query.value(0).toString();
        int weekday=query.value(2).toInt();
        int courseNumber=query.value(3).toInt();
        int coursecount=query.value(4).toInt();
        for(;coursecount>0;coursecount--)
        {
            this->tableWidget->setItem(courseNumber-1,weekday-1,new QTableWidgetItem(message));
            courseNumber++;
        }
    }
    db.close();
}

void MainWindow::showSetSchoolDialog()
{
    dialog.show();
}

void MainWindow::showUpdataCourse(QTableWidgetItem* twi)
{
    updataCourse.loadinfo(twi->text());
    updataCourse.show();
}

void MainWindow::showAddCourse()
{
    //先将旧的信息清除
    addCourse.et_classID->setText("");
    addCourse.et_classname->setText("");
    addCourse.et_classroom->setText("");
    addCourse.et_coursecount->setText("");
    addCourse.et_coursecredit->setText("");
    addCourse.et_dayofweek->setText("");
    addCourse.et_endweek->setText("");
    addCourse.et_otherinfo->setText("");
    addCourse.et_startcoursenumber->setText("");
    addCourse.et_startweek->setText("");
    addCourse.et_teacher->setText("");

    addCourse.show();
}

MainWindow::~MainWindow()
{}
